package com.junsi.web.controller.system;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.junsi.common.annotation.Log;
import com.junsi.common.core.controller.BaseController;
import com.junsi.common.core.domain.AjaxResult;
import com.junsi.common.core.page.TableDataInfo;
import com.junsi.common.enums.BusinessType;
import com.junsi.common.utils.StringUtils;
import com.junsi.common.utils.poi.ExcelUtil;
import com.junsi.framework.util.ShiroUtils;
import com.junsi.system.domain.SysUser;
import com.junsi.system.service.ISysPostService;
import com.junsi.system.service.ISysRoleService;
import com.junsi.system.service.ISysUserService;

/**
 * 用户信息
 * 
 * @author junsi
 */
@Controller
@RequestMapping("/system/user/nd")
public class SysUserNdController extends BaseController
{
	private String prefix = "system/user";

	@Autowired
	private ISysUserService userService;

	@Autowired
	private ISysRoleService roleService;

	@Autowired
	private ISysPostService postService;

	@RequiresPermissions("system:user:view")
	@GetMapping()
	public String user(ModelMap mmap)
	{
		return prefix + "/usernd";
	}

	@RequiresPermissions("system:user:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(SysUser user, String areaCode)
	{
		startPage();
		return getDataTable(userService.selectUserList(user));
	}

	@Log(title = "用户管理", businessType = BusinessType.EXPORT)
	@RequiresPermissions("system:user:export")
	@PostMapping("/export")
	@ResponseBody
	public AjaxResult export(SysUser user)
	{
		List<SysUser> list = userService.selectUserList(user);
		ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class);
		return util.exportExcel(list, "用户数据");
	}

	@Log(title = "用户管理", businessType = BusinessType.IMPORT)
	@RequiresPermissions("system:user:import")
	@PostMapping("/importData")
	@ResponseBody
	public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
	{
		ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class);
		List<SysUser> userList = util.importExcel(file.getInputStream());
		String operName = ShiroUtils.getSysUser().getLoginName();
		String message = userService.importUser(userList, updateSupport, operName);
		return AjaxResult.success(message);
	}

	@RequiresPermissions("system:user:view")
	@GetMapping("/importTemplate")
	@ResponseBody
	public AjaxResult importTemplate()
	{
		ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class);
		return util.importTemplateExcel("用户数据");
	}


	/**
	 * 修改用户
	 */
	@GetMapping("/edit/{userId}")
	public String edit(@PathVariable("userId") Long userId, ModelMap mmap)
	{
		mmap.put("user", userService.selectUserById(userId));
		mmap.put("roles", roleService.selectRolesByUserId(userId));
		mmap.put("posts", postService.selectPostsByUserId(userId));
		return prefix + "/editnd";
	}

	/**
	 * 修改保存用户
	 */
	@RequiresPermissions("system:user:edit")
	@Log(title = "用户管理", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(SysUser user)
	{
		if (StringUtils.isNotNull(user.getUserId()) && SysUser.isAdmin(user.getUserId()))
		{
			return error("不允许修改超级管理员用户");
		}
		user.setUpdateBy(ShiroUtils.getLoginName());
		return toAjax(userService.updateUser(user));
	}

	@RequiresPermissions("system:user:remove")
	@Log(title = "用户管理", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{
		try
		{
			return toAjax(userService.deleteUserByIds(ids));
		}
		catch (Exception e)
		{
			return error(e.getMessage());
		}
	}
	
	@RequiresPermissions("system:user:remove")
	@Log(title = "用户管理", businessType = BusinessType.DELETE)
	@PostMapping("/removeArea")
	@ResponseBody
	public AjaxResult removeArea(Long userId)
	{
		return  toAjax(userService.deleteUserArea(userId));
	}
	
	@RequiresPermissions("system:user:remove")
	@Log(title = "用户管理", businessType = BusinessType.DELETE)
	@PostMapping("/removeAreaAll")
	@ResponseBody
	public AjaxResult removeAreaAll(String userIds)
	{
		return  toAjax(userService.deleteUserAreaAll(userIds));
	}

	/**
	 * 校验用户名
	 */
	@PostMapping("/checkLoginNameUnique")
	@ResponseBody
	public String checkLoginNameUnique(SysUser user)
	{
		return userService.checkLoginNameUnique(user.getLoginName());
	}

	/**
	 * 校验手机号码
	 */
	@PostMapping("/checkPhoneUnique")
	@ResponseBody
	public String checkPhoneUnique(SysUser user)
	{
		return userService.checkPhoneUnique(user);
	}

	/**
	 * 校验email邮箱
	 */
	@PostMapping("/checkEmailUnique")
	@ResponseBody
	public String checkEmailUnique(SysUser user)
	{
		return userService.checkEmailUnique(user);
	}

	/**
	 * 用户状态修改
	 */
	@Log(title = "用户管理", businessType = BusinessType.UPDATE)
	@RequiresPermissions("system:user:edit")
	@PostMapping("/changeStatus")
	@ResponseBody
	public AjaxResult changeStatus(SysUser user)
	{
		return toAjax(userService.changeStatus(user));
	}

	/**
	 * 是否限制区域修改
	 */
	@Log(title = "用户管理", businessType = BusinessType.UPDATE)
	@RequiresPermissions("system:user:edit")
	@PostMapping("/changeisArea")
	@ResponseBody
	public AjaxResult changeisArea(SysUser user)
	{
		return toAjax(userService.changeisArea(user));
	}

    /**
     * 跳转用户列表
     */
	@RequiresPermissions("system:user:view")
	@GetMapping("/userArea")
	public String userArea(String areaCode, ModelMap mmap)
	{
		mmap.put("areaCode", areaCode);
		return prefix + "/userarea";
	}

	/**
	 * 选择用户
	 */
	@GetMapping("/selectUser")
	public String selectUser(String areaCode, ModelMap mmap)
	{
		mmap.put("areaCode", areaCode);
		return prefix + "/selectUser";
	}

	/**
	 * 添加用户
	 */
	@PostMapping("/insertUser")
	@ResponseBody
	public AjaxResult insertUser(String areaCode, String userIds)
	{
		return toAjax(userService.updateUserArea(areaCode , userIds));
	}

    /**
     * 根据区域查询区域内用户
     */
	@RequiresPermissions("system:user:list")
	@PostMapping("/belong")
	@ResponseBody
	public TableDataInfo belong(SysUser user, String areaCode)
	{
		startPage();
		List<SysUser> list = userService.selectUserByArea(areaCode, user);
		return getDataTable(list);
	}

    /**
     * 根据区域查询区域外用户
     */
	@RequiresPermissions("system:user:list")
	@PostMapping("/unbelong")
	@ResponseBody
	public TableDataInfo unbelong(SysUser user, String areaCode)
	{
		startPage();
		List<SysUser> list = userService.selectUserUnByArea(areaCode, user);
		return getDataTable(list);
	}
}